package prepare.huawei;

import java.util.Scanner;

/**
 * description:
 * <a href="https://blog.csdn.net/lq_area/article/details/124778311">华为机试：TLV解析Ⅰ（C语言）</a>
 * 需要了解什么是小端序，如何转化为二进制
 *
 * @author <a href="zhaoyingling12@163.com">simon zhao</a>
 * @since 2023/3/23
 */
public class TlvDecodeI {

    private final static String joinStr = " ";

    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        String tag = input.nextLine();
        String[] tlv = input.nextLine().split(" ");
        int index = 0;
        while (index < tlv.length) {
            // 解析 value 的长度
            int length = parseValueLength(tlv, index);
            // 默认每个解析的都是tag
            if (tag.equals(tlv[index])) {
                StringBuilder builder = new StringBuilder();
                // 跳过两个字节的 length
                int start = index + 3, end = index + 2 + length;
                for (int i = start; i <= end; i++) {
                    builder.append(tlv[i]);
                    if (i != end) {
                        builder.append(joinStr);
                    }
                }
                System.out.println(builder);
                return;
            }
            index = index + 2 + length + 1;
        }
    }

    private static int parseValueLength(String[] tlv, int curIndex) {
        // 小端序列
        return Integer.parseInt(tlv[curIndex + 2] + tlv[curIndex + 1], 16);
    }
}
